package com.ssvendas.fachadas;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.ssvendas.fachadas.remote.QueryMFFRemote;
import com.ssvendas.modelo.Filial;
import com.ssvendas.modelo.Fornecedor;
import com.ssvendas.modelo.Marca;

@Stateless
public class QueryMFF implements QueryMFFRemote{

	@PersistenceContext(unitName = "jeevendas")
	EntityManager em;
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Marca> getMarcas() {
		Query q = em.createQuery("SELECT m FROM Marca m");
		return q.getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Filial> getFiliais() {
		Query q = em.createQuery("SELECT f FROM Filial f");
		return q.getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Fornecedor> getFornecedores() {
		Query q = em.createQuery("SELECT f FROM Fornecedor f");
		return q.getResultList();
	}

	@Override
	public Marca getMarca(String id) {
		Query q = em.createQuery("SELECT m FROM Marca m WHERE m.id=:id");
		q.setParameter("id", id);
		Marca m = null;
		if (q.getResultList().size() > 0)
			m = (Marca) q.getSingleResult();
		
		return m;
	}

	@Override
	public Filial getFilial(String id) {
		Query q = em.createQuery("SELECT f FROM Filial f WHERE f.id=:id");
		q.setParameter("id", id);
		Filial f = null;
		if (q.getResultList().size() > 0)
			f = (Filial) q.getSingleResult();
		
		return f;
	}

	@Override
	public Fornecedor getFornecedor(String id) {
		Query q = em.createQuery("SELECT f FROM Fornecedor f WHERE f.id=:id");
		q.setParameter("id", id);
		Fornecedor f = null;
		if (q.getResultList().size() > 0)
			f = (Fornecedor) q.getSingleResult();
		
		return f;
	}

	@Override
	public Marca getMarcaNome(String nome) {
		Query q = em.createQuery("SELECT m FROM Marca m WHERE m.nome=:nome");
		q.setParameter("nome", nome);
		Marca m = null;
		if (q.getResultList().size() > 0)
			m = (Marca) q.getSingleResult();
		
		return m;
	}

	@Override
	public Filial getFilialNome(String nome) {
		Query q = em.createQuery("SELECT f FROM Filial f WHERE f.nome=:nome");
		q.setParameter("nome", nome);
		Filial f = null;
		if (q.getResultList().size() > 0){
			f = (Filial) q.getSingleResult();
		}
		
		return f;
	}

	@Override
	public Fornecedor getFornecedorNome(String nome) {
		Query q = em.createQuery("SELECT f FROM Fornecedor f WHERE f.nome=:nome");
		q.setParameter("nome", nome);
		Fornecedor f = null;
		if (q.getResultList().size() > 0)
			f = (Fornecedor) q.getSingleResult();
		
		return f;
	}

}
